<template>
  <div class="app-container">
    <!-- 筛选条件 -->
    <el-form v-show="showSearch" ref="queryForm" :model="queryParams" :inline="true" label-width="70px" label-position="left">
      <el-form-item label="埋点名称" prop="eventName">
        <el-input v-model="queryParams.eventName" placeholder="请输入埋点名称" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="埋点标识" prop="eventType">
        <el-input v-model="queryParams.eventType" placeholder="请输入埋点标识" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="埋点说明" prop="enentTrigger">
        <el-input v-model="queryParams.enentTrigger" placeholder="请输入埋点说明" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
    </el-form>

    <!-- 操作按钮 -->
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button v-hasPermi="['system:event:add']" type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button icon="el-icon-refresh-left" size="mini" @click="getList">刷新</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
      </el-col>

      <right-toolbar :show-search.sync="showSearch" @queryTable="getList" />

    </el-row>

    <!-- 表格数据 -->
    <el-table
      v-if="refreshTable"
      v-loading="loading"
      stripe
      border
      :data="businessList"
      :default-expand-all="isExpandAll"
      :tree-props="{ children: 'children', hasChildren: 'hasChildren' }"
      :header-cell-style="{background: $store.state.app.theme.tableBack,color: $store.state.app.theme.tableColor}"
      @cell-dblclick="celldblclick"
    >
      <el-table-column label="埋点名称" align="center" prop="eventName" />
      <el-table-column label="埋点事件标识" align="center" prop="eventType" />
      <el-table-column label="埋点说明(特殊埋点，需要说明用途、存储字段以便分析)" align="center" prop="enentTrigger" width="500px" />
      <el-table-column v-if="checkPermi(['system:event:edit', 'system:event:remove'])" label="操作" align="center">
        <template slot-scope="scope">
          <el-button v-hasPermi="['system:event:edit']" size="mini" type="text" @click="handleUpdate(scope.row)">修改</el-button>
          <el-button v-if="scope.row.parentId != 0" v-hasPermi="['system:event:remove']" size="mini" type="text" @click="handleDelete(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页 -->
    <el-row :gutter="24" class="mb8" style="margin: 20px 0px">
      <el-col :span="24" style="display: flex; justify-content: flex-end; padding: 0px">
        <el-pagination
          background
          layout="prev, pager, next, sizes, total"
          :page-sizes="[10, 50, 100, 200]"
          :page-size="queryParams.pageSize"
          :total="totalPage"
          @current-change="handlePageChange"
          @size-change="
            newSize => {
              queryParams.pageSize = newSize;
              getList();
            }
          "
        />
      </el-col>
    </el-row>

    <!-- 对话框-修改 -->
    <el-dialog :title="titleDialog === 'add' ? '添加埋点信息' : '修改埋点信息'" :visible.sync="openDialog" width="600px" append-to-body>
      <el-form ref="formDialog" :model="formDialog" label-width="120px">
        <el-form-item label="图片例子" prop="img">
          <Up-img :oss-image-url="ossImageUrl" :theme="'rectangular'" :number="1" />
        </el-form-item>
        <el-form-item label="埋点名称" prop="eventName">
          <el-input v-model="formDialog.eventName" placeholder="请输入" />
        </el-form-item>
        <el-form-item label="埋点标识" prop="eventType">
          <el-input v-model="formDialog.eventType" placeholder="请输入" />
        </el-form-item>
        <el-form-item label="埋点说明" prop="enentTrigger">
          <el-input v-model="formDialog.enentTrigger" placeholder="请输入" />
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" :loading="submitLoad" @click="submitForm">确 定</el-button>
        <el-button @click="openDialog = false">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { eventList, delevent, getevent, upevent, addevent } from '@/api/business/buriedPoint'
import '@riophae/vue-treeselect/dist/vue-treeselect.css'
import UpImg from '@/components/UpImg'
import { checkPermi } from '@/utils/permission' // 权限判断函数

export default {
  name: 'BuriedPointEvent',
  components: { UpImg },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 修改渠道loading
      submitLoad: false,
      // 显示搜索条件
      showSearch: true,
      // 表格树数据
      businessList: [],
      // 对话框类型
      titleDialog: '',
      // 是否显示对话框
      openDialog: false,
      // 是否展开，默认全部展开
      isExpandAll: true,
      // 重新渲染表格状态
      refreshTable: true,
      // 查询参数
      queryParams: {
        pageNum: 1, // 当前页数
        pageSize: 10, // 分页大小
        enentTrigger: '',
        eventName: '',
        eventType: ''
      },
      // 上传图片选项
      ossImageUrl: [],
      // 数据总条数
      totalPage: 0,
      // 表单参数
      formDialog: {
        img: '',
        enentTrigger: '',
        eventName: '',
        eventType: '',
        searchValue: ''
      },
      // h5模板数组
      H5Array: [],
      // 极速贷数组
      jisuArray: []
    }
  },
  created() {
    // 分页数据
    this.getList()
  },
  methods: {
    checkPermi,
    // 查询列表
    getList() {
      this.loading = true
      eventList(this.queryParams).then(response => {
        this.businessList = response.rows
        this.totalPage = response.total
        this.loading = false
      }).finally(() => { this.loading = false })
    },
    // 新增按钮操作
    handleAdd() {
      // 如果是新增 重置新增表数据
      if (this.formDialog.id) {
        this.formDialog = this.$options.data().formDialog
        this.ossImageUrl = []
      }
      this.openDialog = true
      this.titleDialog = 'add'
    },
    // 搜索按钮操作
    handleQuery() {
      this.getList()
    },
    // 重置按钮操作
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },
    // 删除按钮操作
    handleDelete(row) {
      this.$modal
        .confirm('是否确认删除')
        .then(function() {
          return delevent(row.id)
        })
        .then(() => {
          this.getList()
          this.$modal.msgSuccess('删除成功')
        })
        .catch(() => {})
    },
    // 修改按钮操作
    handleUpdate(row) {
      this.loading = true
      const upId = row.id
      // logo
      this.ossImageUrl = []
      this.$set(this.ossImageUrl, 0, { url: row.img, id: 23333 })
      getevent(upId).then(response => {
        this.loading = false
        this.openDialog = true
        this.titleDialog = 'up'
        this.formDialog = response.data
      }).finally(() => { this.loading = false })
    },
    // 修改按钮操作
    submitForm() {
      this.submitLoad = true
      this.formDialog.img = this.ossImageUrl[0] ? this.ossImageUrl[0].url : ''
      if (this.titleDialog === 'add') {
        addevent(this.formDialog)
          .then(() => {
            this.submitLoad = false
            this.openDialog = false
            this.getList()
            this.$modal.msgSuccess('新增成功')
          })
          .catch(() => {
            this.submitLoad = false
          })
      } else {
        upevent(this.formDialog)
          .then(() => {
            this.submitLoad = false
            this.openDialog = false
            this.getList()
            this.$modal.msgSuccess('修改成功')
          })
          .catch(() => {
            this.submitLoad = false
          })
      }
    },
    // 表单重置
    reset() {
      this.form = {
        parentId: '',
        deptName: '',
        pdAmount: '',
        tag: '',
        leader: '',
        type: '',
        url: '',
        zkStatus: '1'
      }
      this.resetForm('form')
    },
    // 分页请求
    handlePageChange(data) {
      this.queryParams.pageNum = data
      this.getList()
    },
    // 双击复制
    celldblclick(row, column, cell, event) {
      if (!column.property) return
      if (!row[column.property]) return
      const oInput = document.createElement('input')
      oInput.value = row[column.property]
      document.body.appendChild(oInput)
      oInput.select()
      document.execCommand('Copy')
      this.$message.success({
        message: '复制成功',
        showClose: true
      })
      oInput.remove()
    }
  }
}
</script>
